package com.rocksdata.mc.ui.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import com.ctz.commonframe.ui.BaseFragment;
import com.ctz.commonframe.utils.DateUtil;
import com.rocksdata.mc.R;
import com.rocksdata.mc.business.UserInfoManager;
import com.rocksdata.mc.entity.CourseInfo;
import com.rocksdata.mc.entity.UserInfo;
import com.rocksdata.mc.view.DayCourseView;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 陈堂正 on 2017/2/13.
 */
@ContentView(R.layout.fragment_day_timetable)
public class DayTimetableFragment extends BaseFragment {
    @ViewInject(R.id.tv_day_of_week)
    private TextView tvDayOfWeek;
    @ViewInject(R.id.tv_date)
    private TextView tvDate;
    @ViewInject(R.id.zcv_morning)
    private DayCourseView dcvMorning;
    @ViewInject(R.id.zcv_afternoon)
    private DayCourseView dcvAfternoon;
    @ViewInject(R.id.zcv_evening)
    private DayCourseView dcvEvening;

    private int currentDayOfWeekIndex;
    private List<CourseInfo> currentCourseList = new ArrayList<CourseInfo>();
    private long currentDayMillis;

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
//        dcvMorning.setZone(CourseInfo.DAY_ZONE_MORNING);
//        dcvAfternoon.setZone(CourseInfo.DAY_ZONE_AFTERNOON);
//        dcvEvening.setZone(CourseInfo.DAY_ZONE_EVENING);
        Bundle bundle = getArguments();
        if(bundle != null) {
            currentCourseList = (List<CourseInfo>) bundle.getSerializable("timetable_data");
            currentDayMillis = bundle.getLong("timetable_date");
        }
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    protected void onVisible() {
        super.onVisible();
        setupView();
    }

    private void handleSameCourse(List<CourseInfo> list){
        List<CourseInfo> temp = new ArrayList<CourseInfo>();
        int userType = UserInfoManager.getCurrentUserType();
        CourseInfo lastInfo = null;
        if(list != null && list.size() > 0) {
            for(int i = list.size() - 1; i > 0; i--) {
                CourseInfo info = list.get(i);
                if(lastInfo != null){
                    boolean isSame = false;
                    if(lastInfo.getLesson() - info.getLesson() == 1 && info.getClassCode().equals(lastInfo.getCourseCode())){
                        if(userType == UserInfo.USER_TYPE_STUDENT){
                            isSame = true;
                        }else if(userType == UserInfo.USER_TYPE_TEACHER && info.getClassCode().equals(lastInfo.getClassCode())){
                            isSame = true;
                        }
                    }
//                    if(isSame){
//                        info.duration += lastInfo.duration;
//                        temp.add(lastInfo);
//                    }
                }
                lastInfo = info;
            }
            list.removeAll(temp);
        }
    }

    private void setupView(){
        if(currentDayMillis > 0) {
            currentDayOfWeekIndex = DateUtil.getDayOfWeekIndex(currentDayMillis);
            tvDate.setText(DateUtil.millis2String(currentDayMillis, DateUtil.DATE_FORMAT_YYYY_MM_DD_CN));
            tvDayOfWeek.setText(DateUtil.getDayOfWeek(currentDayOfWeekIndex));
        }
        if(currentCourseList != null) {
            handleSameCourse(currentCourseList);
            dcvMorning.setData(currentDayMillis, currentCourseList);
            dcvAfternoon.setData(currentDayMillis, currentCourseList);
            dcvEvening.setData(currentDayMillis, currentCourseList);
        }
    }

    public void setDatas(List<CourseInfo> list, long dayMillis){
        currentCourseList = list;
        currentDayMillis = dayMillis;
        setupView();
    }
}
